www.gusucode.com > VC编写的串口调试软件 > VC编写的串口调试软件,内含Modbus协议类 支持对Modbus通讯调试/Modbus串口调试软件1.0/com/Pjsecure.cpp

    /*
Module : PJSECURE.CPP
Purpose: Implementation for a number of functions which
         can be used as the basis for a secuity scheme 
         for a networked product of yours
Created: PJN / 18-04-1998
History: PJN / 28-04-2000 1. Now calls UuidCreateSequential instead of UuidCreate if we
                          are running on Windows 2000
                          2. Code is now Unicode compliant and build configurations 
                          are provided.

Copyright (c) 1998 - 2000 by PJ Naughter.  
All rights reserved.

*/

#include "stdafx.h"

///////////////////////////////// Includes //////////////////////////////////
#include <afxwin.h>
#include <rpc.h>
#include "pjsecure.h"
 

//////////////////////////////// implementation /////////////////////////////




//Class which handles UuidCreateSequential call, which 
//must be constructed at run time since it is not implemented on NT 4 or Windows 9x,
//
//Note that I could have used VC 6's support for delay loading but this would
//mean that the code would not work on VC 5 which I aim to provide support
//for in my code.

class _UUIDS
{
public:
//Constructors /Destructors
  _UUIDS();

//typedefs of the function pointers
  typedef RPC_STATUS (WINAPI UUIDCREATESEQUENTIAL)(UUID*);
  typedef UUIDCREATESEQUENTIAL* LPUUIDCREATESEQUENTIAL;

//Member variables
  HINSTANCE              m_hRcpt4;  //Instance handle of the "RCPT4.DLL" which houses the UuidCreateSequential
  LPUUIDCREATESEQUENTIAL m_lpfnUuidCreateSequential;
};

_UUIDS::_UUIDS()
{
  m_hRcpt4 = GetModuleHandle(_T("RPCRT4.DLL"));
  VERIFY(m_hRcpt4 != NULL);

  m_lpfnUuidCreateSequential = (LPUUIDCREATESEQUENTIAL) GetProcAddress(m_hRcpt4, "UuidCreateSequential");
}

//The local variable which handle the function pointers
_UUIDS _uuids;


BOOL GetNicAddress(LPTSTR pszNicAddress, UINT nBufSize)
{
  BOOL bSuccess = FALSE;

  //NIC address is 12 character string
  if (nBufSize < 13)  
    return FALSE;

  //the way we determine the NIC address is to call the RPC DCE function
  //UuidCreate. The standard format of the GUID returned contains
  //the NIC address in the last 12 characters. The added advantage to
  //this method is that we do not need to rely on a specific network
  //protocol needing to be installed on the client machine to determine
  //the NIC address. You could use this function as the basis for a
  //security scheme for a networked product of yours. Using the NIC address
  //is a guranteed way of uniquely identify a machine throughout the network.
  //
  //One thing to note is that we call UuidCreateSequential if it is available.
  //This is due to a change in the way that UuidCreate works on Windows 2000

  UUID Uuid;
  RPC_STATUS rpcStatus;
  if (_uuids.m_lpfnUuidCreateSequential)
    rpcStatus = _uuids.m_lpfnUuidCreateSequential(&Uuid);
  else
    rpcStatus = UuidCreate(&Uuid);
  if (rpcStatus == RPC_S_OK)
  {
    #ifdef _UNICODE
    unsigned short* pszGuid;  
    #else
    unsigned char* pszGuid;  
    #endif
    rpcStatus = UuidToString(&Uuid, &pszGuid);
    if (rpcStatus == RPC_S_OK)
    {
      TCHAR* pLastMinus = _tcsrchr((TCHAR*)pszGuid, _T('-'));
      if (pLastMinus)
      {
        _tcscpy(pszNicAddress, pLastMinus+1);
        bSuccess = TRUE;
      }

      //need to free created buffer
      RpcStringFree(&pszGuid);
    }
    else 
      TRACE(_T("Error calling UuidToString, Error value is %d\n"), rpcStatus);
  }
  else
    TRACE(_T("Error calling UuidCreate, Error value is %d\n"), rpcStatus);

  return bSuccess;
}


BOOL GetCDriveSerialNumber(LPDWORD pdwSerialNumber)
{
  //Again as with the NIC address, the serial number of the C drive
  //could be used as the basis of a security scheme for a networked 
  //product of yours
  BOOL bSuccess = (GetVolumeInformation(_T("C:\\"), NULL, 0, pdwSerialNumber,
                                        NULL, NULL, NULL, 0) != 0);
  return bSuccess;
}